package main.java.com.lee.threadlocal;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2021/12/13
 */
public class ThreadLocalDemo1 {
	public static void main(String[] args) {
//		ThreadLocal<User> threadLocal = new ThreadLocal<>();
		ThreadLocal<User> threadLocal = ThreadLocal.withInitial(User::new);
		ExecutorService executorService = Executors.newFixedThreadPool(10);

		IntStream.rangeClosed(1, 10).forEach(i->{
			executorService.submit(()->{
				IntStream.rangeClosed(1, 10).forEach(j->{
					if (threadLocal.get() == null) {
						threadLocal.set(new User());
					} else {
						threadLocal.get().setAge(threadLocal.get().getAge() + 1);
					}
				});
				System.out.println(threadLocal.get().getAge());
			});
		});

		executorService.shutdown();
	}

	static class User{

		public User(){};
		private int age;

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}
	}
}
